package com.mlizhi.widgets.cursor4water;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.FontMetrics;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mlizhi.kingdom.R;

public class RangeView extends RelativeLayout {
	
	//字体大小
	private float bgTextSize = 10.0f;
	
	//干燥、正常、湿润区间
	private TextView dryLabel,normalLabel,moistLabel;
	
	//背景宽度
	private float bgViewWidth = 0f;
	
	//干燥、正常和湿润背景颜色和文字颜色
	private int dryColor, normalColor,moistColor,textColor;
	
	//干燥、正常和湿润区间向右偏移量
	private static final float OFFSET_RIGHT = 0f;

	private final int DEFAULT_TEXT_COLOR = Color.parseColor("#3A3A3A");
	
	//干燥、正常、湿润区间边界的
	private TextView maxScaleText,minScaleText,normalMaxScaleText,normalMinScaleText;

	public RangeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public RangeView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RangeView(Context context) {
		super(context);
	}
	
	
	/**
	 * 初始化组件
	 * @param context
	 */
	public void initRangeView(Context context){
		//干燥区间
		RelativeLayout.LayoutParams param4dry =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		param4dry.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		param4dry.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		dryLabel = new TextView(getContext());
		dryLabel.setText(R.string.detect_result_dry);
		dryLabel.setTextColor(textColor);
		dryLabel.setGravity(Gravity.CENTER);
		addView(dryLabel,param4dry);

		//正常区间
		RelativeLayout.LayoutParams param4normal =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		param4normal.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		param4normal.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		normalLabel = new TextView(getContext());
		normalLabel.setText(R.string.detect_result_medium);
		normalLabel.setTextColor(textColor);
		normalLabel.setBackgroundColor(normalColor);
		addView(normalLabel,param4normal);

		//湿润区间
		RelativeLayout.LayoutParams param4moist =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		param4moist.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		param4moist.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		moistLabel = new TextView(getContext());
		moistLabel.setText(R.string.detect_result_moisture);
		moistLabel.setTextColor(textColor);
		moistLabel.setBackgroundColor(moistColor);
		addView(moistLabel,param4moist);
		
		//正常区间右边界的刻度
		normalMaxScaleText = new TextView(getContext());
		addView(normalMaxScaleText);
				
		//正常区间左边界的刻度
		normalMinScaleText = new TextView(getContext());
		addView(normalMinScaleText);
		
		//湿润区间右边界的刻度
		maxScaleText = new TextView(getContext());
		addView(maxScaleText);
		
		//干燥区间左边界的刻度
		minScaleText = new TextView(getContext());
		addView(minScaleText);
	}
	
	
	
	/**
	 * 根据不同部位，设置区间范围
	 * @param minScale 最小刻度
	 * @param normalMinScale 正常区间的最小刻度
	 * @param normalMaxScale 正常区间的最大刻度
	 * @param maxScale 最大刻度
	 * @param showScale 是否显示刻度值
	 */
	public void setMeasureWidth(float minScale,float normalMinScale,float normalMaxScale,float maxScale,boolean showScale) {
		if(showScale){
			minScaleText.setText(String.valueOf(minScale+"%"));
			normalMinScaleText.setText(String.valueOf(normalMinScale+"%"));
			normalMaxScaleText.setText(String.valueOf(normalMaxScale+"%"));
			maxScaleText.setText(String.valueOf(maxScale+"%"));
		}else {
			minScaleText.setText("");
			normalMinScaleText.setText("");
			normalMaxScaleText.setText("");
			maxScaleText.setText("");
		}

		minScaleText.setTextColor(DEFAULT_TEXT_COLOR);
		normalMinScaleText.setTextColor(DEFAULT_TEXT_COLOR);
		normalMaxScaleText.setTextColor(DEFAULT_TEXT_COLOR);
		maxScaleText.setTextColor(DEFAULT_TEXT_COLOR);
		//为获取字体高度
		TextPaint tp = minScaleText.getPaint();
		FontMetrics fm = tp.getFontMetrics();
//		int textWidth = (int) (Math.ceil(fm.descent - fm.ascent) + 2); 
		int textWidth = (int) tp.measureText(String.valueOf(minScale+"%"));
		int textHeight = (int) (Math.ceil(fm.bottom -fm.top)+16);
		
		//干燥区间
		ViewGroup.LayoutParams param4dry = dryLabel.getLayoutParams();
		dryLabel.setTextSize(bgTextSize);
		dryLabel.setBackgroundColor(dryColor);
		if (param4dry != null) {
			((LayoutParams) param4dry).leftMargin = (int) (bgViewWidth * 0 + OFFSET_RIGHT);
			
			((LayoutParams) param4dry).height = textHeight;
			((LayoutParams) param4dry).width = (int) (bgViewWidth * ((normalMinScale-minScale)/(maxScale-minScale)));
		}
		
		//正常区间
		ViewGroup.LayoutParams param4normal = normalLabel.getLayoutParams();
		normalLabel.setTextSize(bgTextSize);
		normalLabel.setGravity(Gravity.CENTER);
		if (param4normal != null) {
			((LayoutParams) param4normal).leftMargin = (int) (bgViewWidth * ((normalMinScale-minScale)/(maxScale-minScale))+ OFFSET_RIGHT);
			((LayoutParams) param4normal).height = textHeight;
			((LayoutParams) param4normal).width = (int) (bgViewWidth * ((normalMaxScale-normalMinScale)/(maxScale-minScale)));
		}
		
		
		//湿润区间
		ViewGroup.LayoutParams param4moist = moistLabel.getLayoutParams();
		moistLabel.setTextSize(bgTextSize);
		moistLabel.setGravity(Gravity.CENTER);
		if (param4moist != null) {
			((LayoutParams) param4moist).leftMargin = (int) (bgViewWidth * ((normalMaxScale-minScale)/(maxScale-minScale))+ OFFSET_RIGHT);
			((LayoutParams) param4moist).height = textHeight;
			((LayoutParams) param4moist).width = (int) (bgViewWidth * ((maxScale-normalMaxScale)/(maxScale-minScale)));
		}
		
		
		//左边界的刻度
		ViewGroup.LayoutParams param4minScale = minScaleText.getLayoutParams();
		minScaleText.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
		if (param4minScale != null) {
			((LayoutParams) param4minScale).leftMargin = 0;
			((LayoutParams) param4minScale).topMargin = textHeight;
		}
		
		//正常区间左边界的刻度
		ViewGroup.LayoutParams param4normalMinScale = normalMinScaleText.getLayoutParams();
		normalMinScaleText.setGravity(Gravity.CENTER);
		if (param4normalMinScale != null) {
			((LayoutParams) param4normalMinScale).leftMargin = (int) (bgViewWidth * ((normalMinScale-minScale)/(maxScale-minScale)) - textWidth * 0.5f);
			((LayoutParams) param4normalMinScale).topMargin = textHeight;
		}

		//正常区间右边界的刻度
		ViewGroup.LayoutParams param4normalMaxScale = normalMaxScaleText.getLayoutParams();
		normalMaxScaleText.setGravity(Gravity.CENTER);
		if (param4normalMaxScale != null) {
			((LayoutParams) param4normalMaxScale).leftMargin = (int) (bgViewWidth * ((normalMaxScale-minScale)/(maxScale-minScale)) - textWidth * 0.5f);
			((LayoutParams) param4normalMaxScale).topMargin = textHeight;
			
		}
		
		
		//右边界的刻度
		ViewGroup.LayoutParams param4maxScale = maxScaleText.getLayoutParams();
		maxScaleText.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
		if (param4maxScale != null) {
			((LayoutParams) param4maxScale).leftMargin = (int) (bgViewWidth - textWidth*1.2f );
			((LayoutParams) param4maxScale).topMargin = textHeight;
		}
	}

	public float getBgTextSize() {
		return bgTextSize;
	}

	public void setBgTextSize(float bgTextSize) {
		this.bgTextSize = bgTextSize;
	}

	public float getBgViewWidth() {
		return bgViewWidth;
	}

	public void setBgViewWidth(float bgViewWidth) {
		this.bgViewWidth = bgViewWidth;
	}

	public int getDryColor() {
		return dryColor;
	}

	public void setDryColor(int dryColor) {
		this.dryColor = dryColor;
	}

	public int getNormalColor() {
		return normalColor;
	}

	public void setNormalColor(int normalColor) {
		this.normalColor = normalColor;
	}

	public int getMoistColor() {
		return moistColor;
	}

	public void setMoistColor(int moistColor) {
		this.moistColor = moistColor;
	}

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}
	
	
	
	
}
